package com.example.dessert.controller.fore;

import com.example.dessert.annotation.LoginCheck;
import com.example.dessert.controller.BaseController;
import com.example.dessert.entity.Cart;
import com.example.dessert.service.CartService;
import com.example.dessert.util.ResponsePojo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: CartController
 * @Description:
 * @Author li
 * @Date 2022/1/14
 * @Version 1.0
 */
@Controller
@RequestMapping("/fore/cart")
public class CartController extends BaseController  {

    @Resource
    private CartService cartService;

    /**
     * 加入购物车
     */
    @PostMapping("/addCart")
    @ResponseBody
    public ResponsePojo addCart(HttpSession session, @RequestParam Map<String, Object> map) {

        Object userId = session.getAttribute("userId");
        Long productId = Long.parseLong(map.get("productId").toString());
        Long specId = Long.parseLong(map.get("specId").toString());
        Integer number = Integer.parseInt(map.get("number").toString());


        //查询是否已经加购
        Cart userCart= cartService.queryCartInfo((Long) userId, productId, specId);
        logger.info("Query whether the user's shopping cart has been added to the product results:{}", userCart);

        if(userCart != null){
            number = userCart.getNumber() + number;
            Cart cart = new Cart();
            cart.setId(userCart.getId())
                    .setNumber(number);
            cartService.updateCartById(cart);

            return ResponsePojo.ok();
        }

        Cart cart = new Cart();
        cart.setUserId((Long) userId)
            .setProductId(productId)
            .setSpecId(specId)
            .setNumber(number);
        Long result =  cartService.storeCart(cart);
        logger.info("User favorite product results:{}", result);

        return ResponsePojo.ok();

    }


    /**
     * 购物车列表
     */
    @GetMapping("/list")
    @LoginCheck
    public String list(HttpSession session, Model model) {

        Object userId = session.getAttribute("userId");

        List<Cart> carts= cartService.queryCartList((Long) userId);
        logger.info("Query user cart List results :{}", carts);

        model.addAttribute("carts", carts);

        return "fore/cart/list";

    }


    /**
     * 购物车删除
     */
    @PostMapping("/delCart")
    @ResponseBody
    public ResponsePojo delCart(HttpSession session, @RequestParam Map<String, Object> map) {

        logger.info("User deletes cart parameters based on id:{}", map);

        Object userId = session.getAttribute("userId");
        Long cartId = Long.parseLong(map.get("cartId").toString());

        int result =  cartService.cancelCart(cartId);
        logger.info("User deletes cart result based on id:{}", result);

        return ResponsePojo.ok();

    }


    /**
     * 购物车数量修改
     */
    @PostMapping("/saveCartNumber")
    @ResponseBody
    public ResponsePojo saveCartNumber(HttpSession session, @RequestParam Map<String, Object> map) {

        logger.info("User save cart number parameters :{}", map);

        Object userId = session.getAttribute("userId");
        Long cartId = Long.parseLong(map.get("cartId").toString());
        Integer number = Integer.parseInt(map.get("number").toString());

        Cart cart = new Cart();
        cart.setId(cartId)
                .setNumber(number);
        cartService.updateCartById(cart);

        return ResponsePojo.ok();

    }



}
